#include <memory.h>
#include <stdlib.h>
#include "vol_d3des.h"

/*******************************************/

static void getKey (unsigned char* key, char* szPassword)
{
	unsigned char *pk, *pe;
	char* p = szPassword;

	pk = key;
	memset (pk, 0, 8);
	pe = pk + 8;

	while (*p != '\0')
	{
		*pk++ ^= (unsigned char)*p++;
		if (pk == pe)
			pk = key;
	}
}

/* !!! nDataSize ������ 8 �������� */
static void run (unsigned char* pd, int nDataSize, char* szPassword, int blEncrypt)
{
	int nCount, i;

    unsigned char key [8];
	getKey (key, szPassword);

	if (blEncrypt)
		deskey (key, EN0);
	else
		deskey (key, DE1);

	nCount = nDataSize / 8;

	for (i = 0; i < nCount; i++)
	{
		des (pd, pd);
		pd += 8;
	}
}

// ���������pd�е�����.
void vol_des_encrypt (unsigned char* pd, int nDataSize, char* szPassword)
{
	run (pd, nDataSize, szPassword, 1);
}

// ���������pd�е�����.
void vol_des_decrypt (unsigned char* pd, int nDataSize, char* szPassword)
{
	run (pd, nDataSize, szPassword, 0);
}
